package com.hui.auth.service.impl;

import com.hui.auth.config.shiro.SecurityUtil;
import com.hui.auth.dao.AuthPermissionDao;
import com.hui.auth.dao.AuthRoleDao;
import com.hui.auth.dao.AuthUserDao;
import com.hui.auth.entity.AuthPermission;
import com.hui.auth.entity.AuthRole;
import com.hui.auth.entity.AuthUser;
import com.hui.auth.service.AuthUserService;
import com.hui.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    private AuthUserDao authUserDao;
    @Autowired
    private AuthRoleDao authRoleDao;
    @Autowired
    private AuthPermissionDao authPermissionDao;

    @Override
    @Transactional
    public boolean insert(AuthUser user, List<Integer> roleIds) {
        String userName = SecurityUtil.currentUser().getUserName();
        String date = DateUtil.parseDateToString(new Date());
        String inPassword = "123456";
        user.setPassword(DigestUtils.md5DigestAsHex(inPassword.getBytes()));
        user.setCreateTime(date);
        user.setCreateUser(userName);
        user.setUpdateTime(date);
        user.setUpdateUser(userName);
        user.setLastLogin(null);
        if (authUserDao.insert(user)) {
            authUserDao.insertUserRole(user.getId(), roleIds);
            return true;
        }
        return false;
    }

    @Override
    public List<AuthUser> selectAll(Map<String, Object> params) {
        return authUserDao.selectAll(params);
    }

    @Override
    public boolean update(AuthUser user) {
        if (1 == authUserDao.update(user)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(AuthUser user, List<Integer> roleIds) {
        if (1 == authUserDao.update(user)) {
            authUserDao.deleteRoleByUid(user.getId());
            authUserDao.insertUserRole(user.getId(), roleIds);
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        if (1 == authUserDao.delete(id)) {
            authUserDao.deleteRoleByUid(id);
            return true;
        }
        return false;
    }

    public AuthUser selectByUserName(String username) {
        AuthUser user = authUserDao.selectByUserName(username);
        if (null != user) {
            List<AuthRole> roles = authRoleDao.selectByUid(user.getId());
            StringBuilder ids = new StringBuilder("");
            for (AuthRole role : roles) {
                if (StringUtils.isEmpty(ids.toString())) {
                    ids.append(String.valueOf(role.getId()));
                } else {
                    ids.append(",");
                    ids.append(String.valueOf(role.getId()));
                }
            }
            List<AuthPermission> permissions = authPermissionDao.selectByIds(ids.toString());
            user.setRoles(roles);
            user.setPermissions(permissions);
        }
        return user;
    }

    @Override
    public Map select(String username) {
        Map<String, Object> result = new HashMap();
        AuthUser user = authUserDao.selectByUserName(username);
        List<Integer> roles = null;
        if (null != user) {
            roles = authRoleDao.selectIds(user.getId());
        }
        result.put("user", user);
        result.put("roleIds", roles);
        return result;
    }

}
